<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Article extends Admin_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->library('pagination');
        $this->load->model('article_m');
        $this->load->model('car_m');
    }

    public function index($page = 1)
    {
        //分页
        $where                      = [];
        $limit                      = 20;
        $config['uri_segment']      = 4;
        $config['use_page_numbers'] = true;
        $config['total_rows']       = $this->article_m->total($where);
        $config['base_url']         = base_url('manage/Article/index');
        $config['per_page']         = $limit;
        $config['num_links']        = 1;
        $start                      = ($page - 1) * $limit;
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        $articles           = $this->article_m->select($where, $start, $limit);
        if ($articles) {
            foreach ($articles as $key => $val) {
                if(!empty($val['car_category'])){
                    $car =  $this->car_m->find($val['car_category']);
                    $articles[$key]['car_category'] = $car['car_name'];
                }else{
                    $articles[$key]['car_category'] = "--";
                }

            }
        }
        $data['articles']   = $articles;
        $this->load->view('article/index', $data);
    }

    public function add()
    {
        $data['pic_path'] = "[]";
        $data['pic_con']  = json_encode([]);
        $data['car']      = $this->car_m->select();
        $this->load->view('article/form', $data);
    }

    public function edit($article_id = null)
    {
        $article = $this->article_m->find($article_id);
        if (!$article) {
            show_message('参数错误！', site_url(X_URL . 'Article/index'));
        }
        $del_url = base_url("index.php/Upload/delete_img?url=" . $article['img']);;
        $pic_con[]          = [
            'caption' => "轮播图",
            'size'    => 576237,
            'width'   => '120px',
            'url'     => $del_url,
            "key"     => $article['id'],
        ];
        $data['pic_path']   = "['" . $article['img'] . "']";
        $data['pic_con']    = json_encode($pic_con);
        $data['article']    = $article;
        $data['article_id'] = $article['id'];
        $data['car']        = $this->car_m->select();
        $this->load->view('article/form', $data);
    }

    public function del($article_id)
    {
        if ($article_id) {
            if ($this->article_m->delete_by(['id' => $article_id])) {
                show_message('恭喜，删除成功', site_url(X_URL . 'Article/index'), 1);
            } else {
                show_message('参数错误', site_url(X_URL . 'Article/index'));
            }
        } else {
            show_message('参数错误', site_url(X_URL . 'Article/index'));
        }
    }

    public function update()
    {

        $post          = $this->input->post();
        $article_title = isset($post['title']) ? htmlspecialchars($post['title'], ENT_QUOTES) : false;
        $article_id    = isset($post['article_id']) ? intval($post['article_id']) : false;
        if (empty($post['car_category'])) {
            show_message('请选择所属车型', site_url(X_URL . 'Article/add'));
        }
        $article = [
            'title'        => $article_title,
            'car_category' => $post['car_category'] ?? "",
            'author'       => $post['author'] ?? "",
            'img'          => $post['img'] ?? "",
            'content'      => $post['content'] ?? "",
        ];
        if (!$article_id) {

            // 新增
            $article['create_time'] = date("Y-m-d H:i:s", time());
            $articles               = $this->article_m->insert($article);
            if ($articles) {
                show_message('新增文章成功', site_url(X_URL . 'Article/index'), 1);
            } else {
                show_message('新增文章失败', site_url(X_URL . 'Article/add'));
            }
        } else {
            // 修改文章信息
            $affected_rows = $this->article_m->update(['id' => $article_id], $article);
            if ($affected_rows) {
                show_message('修改文章成功', site_url(X_URL . 'Article/index'), 1);
            } else {
                show_message('修改文章失败', site_url(X_URL . 'Article/edit'));
            }
        }
    }
}

